﻿package net.wangit.dev.unit;

import java.util.HashMap;
import java.util.Map;

import net.wangit.LabelUtil;
import net.wangit.context.ActionContext;
import net.wangit.support.Validator;
import net.wangit.util.Checker;
import net.wangit.util.StringUtil;


public class ElementIdValidator implements Validator {

	private static final Map<String, Object> keywords;
	private static final Map<String, Object> valid_characters;

	public ElementIdValidator() {
	}

	public String validate(ActionContext ac, String value, String expression,
			int rowIndex) {
		value = StringUtil.clearSpace(value);
		if (Checker.isEmpty(value))
			return null;
		if (keywords.containsKey(value.toLowerCase()))
			return LabelUtil.get(ac, "dev", "validation.element_id.keywords",
					value);
		String firstChar = value.substring(0, 1);
		boolean pass = !Checker.isInteger(firstChar);
		for (int i = 0; i < value.length(); i++) {
			String character = value.substring(i, i + 1);
			if (valid_characters.containsKey(character))
				continue;
			pass = false;
			break;
		}

		return pass ? null : LabelUtil.get(ac, "dev",
				"validation.invalid_element_id");
	}

	public String validate(ActionContext ac, String from, String to, String s) {
		return null;
	}

	static {
		keywords = new HashMap<String, Object>();
		valid_characters = new HashMap<String, Object>();
		keywords.put("m", null);
		keywords.put("p", null);
		valid_characters.put("_", null);
		valid_characters.put("0", null);
		valid_characters.put("1", null);
		valid_characters.put("2", null);
		valid_characters.put("3", null);
		valid_characters.put("4", null);
		valid_characters.put("5", null);
		valid_characters.put("6", null);
		valid_characters.put("7", null);
		valid_characters.put("8", null);
		valid_characters.put("9", null);
		valid_characters.put("a", null);
		valid_characters.put("b", null);
		valid_characters.put("c", null);
		valid_characters.put("d", null);
		valid_characters.put("e", null);
		valid_characters.put("f", null);
		valid_characters.put("g", null);
		valid_characters.put("h", null);
		valid_characters.put("i", null);
		valid_characters.put("j", null);
		valid_characters.put("k", null);
		valid_characters.put("l", null);
		valid_characters.put("m", null);
		valid_characters.put("n", null);
		valid_characters.put("o", null);
		valid_characters.put("p", null);
		valid_characters.put("q", null);
		valid_characters.put("r", null);
		valid_characters.put("s", null);
		valid_characters.put("t", null);
		valid_characters.put("u", null);
		valid_characters.put("v", null);
		valid_characters.put("w", null);
		valid_characters.put("x", null);
		valid_characters.put("y", null);
		valid_characters.put("z", null);
		valid_characters.put("A", null);
		valid_characters.put("B", null);
		valid_characters.put("C", null);
		valid_characters.put("D", null);
		valid_characters.put("E", null);
		valid_characters.put("F", null);
		valid_characters.put("G", null);
		valid_characters.put("H", null);
		valid_characters.put("I", null);
		valid_characters.put("J", null);
		valid_characters.put("K", null);
		valid_characters.put("L", null);
		valid_characters.put("M", null);
		valid_characters.put("N", null);
		valid_characters.put("O", null);
		valid_characters.put("P", null);
		valid_characters.put("Q", null);
		valid_characters.put("R", null);
		valid_characters.put("S", null);
		valid_characters.put("T", null);
		valid_characters.put("U", null);
		valid_characters.put("V", null);
		valid_characters.put("W", null);
		valid_characters.put("X", null);
		valid_characters.put("Y", null);
		valid_characters.put("Z", null);
	}
}
